package com.aring.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.aring.config.FileServerConfig;
import com.aring.utils.FileProcessor;

@Controller
@RequestMapping("upload/")
public class FileUploadController {

	@Autowired
	private FileServerConfig fileServerConfig;
	
	@RequestMapping(value="image",method=RequestMethod.POST)
	public  ModelAndView image(@RequestParam("file") MultipartFile file){
		ModelAndView mView = new ModelAndView("error");
		try{
		String type = file.getContentType();
		type = type.split("/")[0];
		String name = FileProcessor.save(fileServerConfig.getImagePath(), file);
		String url = "http://"+fileServerConfig.getHost()+":"+fileServerConfig.getPort()+"/images/"+name;
		mView.setViewName("success");
		mView.addObject("message", "文件上传成功");
		mView.addObject("next", url);
		System.out.println(url);
		return mView;
		}catch(Exception e){
			e.printStackTrace();
			mView.addObject("message", e.getMessage());
			return mView;
		}
	}
	
	
	
}
